package com.qububu.play.dineTogether.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.play.dineTogether.dto.ActionFoodDto;
import com.qububu.play.dineTogether.dto.ActionFoodDto.StapleFood;
import com.qububu.play.dineTogether.service.DineFoodService;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

@Controller
@RequestMapping("/play/dine")
public class DineFoodControl {

	@Resource
	private DineFoodService dineFoodService;

	/**
	 * @功能 查看对应action_id下的foodList
	 */
	@RequestMapping("/getFoods")
	@ResponseBody
	public Map<String, Object> queryFoodList(HttpServletRequest request, HttpServletResponse response) {
		String actionId = SystemUtil.decrypt(ParamConverter.assertNotEmpty(request.getParameter("actionId")));
		int reActionId = ParamConverter.convertToInteger(actionId);
		
		List<ActionFoodDto> foodList = dineFoodService.getActionFoodListByActionId(reActionId);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("foods", foodList);
		
		return resultMap;
	}
	
	
	/**
	 * @功能 根据foodId查看食物详情
	 */
	@RequestMapping("/getFoodInfo")
	public String queryFoodInfoById(HttpServletRequest request, HttpServletResponse response){
		
		String foodId = ParamConverter.assertNotEmpty(request.getParameter("foodId"));
		int foodIdInt = ParamConverter.convertToInteger(SystemUtil.decrypt(foodId));
		
		ActionFoodDto actionFoodDto = dineFoodService.getActionFoodInfoByFoodId(foodIdInt);
		request.setAttribute("actionFoodInfo", actionFoodDto);
	
		return "testFoodDetal";
	}
	
	/*****************************************管理员用*********************************************/
	
	/**
	 * 进入添加食物的界面, 后面可能会和DineStaffControl中进入添加页面相似,改成带参数的重定向
	 * */
	@RequestMapping("/admin/infoAddFood")
	public String infoAddFood(HttpServletRequest req) {
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		req.setAttribute("actionId", actionId);
		return "adminDineFoods";
	}
	
	/**
	 * @功能 添加食物
	 * @操作表 action_food
	 * @备注 怎么添加多种食物
	 * @param actionId 怎么得到，是和userName一样？？？？
	 */
	@RequestMapping("/admin/ajaxAddActionFood")
	@ResponseBody()
	public Map<String, Object> doAddActionFood (HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> addFoodMap = new HashMap<String, Object>();
		
		String foodId = SystemUtil.getUUIDString();
		String stapleFood = ParamConverter.assertNotEmpty(request.getParameter("stapleFood"));
		String foodPic = ParamConverter.assertNotEmpty(request.getParameter("foodPic"));
		String foodName = SystemUtil.convertSpecialMark(ParamConverter.assertNotEmpty(request.getParameter("foodName")));
		String actionId = ParamConverter.assertNotEmpty(request.getParameter("actionId"));
		
		ActionFoodDto actionFoodDto = createActionFoodEntity(foodId, stapleFood, foodPic, foodName, actionId);
		if(dineFoodService.doAddActionFood(actionFoodDto)) {
			addFoodMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			addFoodMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			addFoodMap.put("message", "缓存操作失败, 请稍后重试");
		}
		
		return addFoodMap;
	}
	
	/**
	 * @功能 创建actionFoodDto类
	 * @return
	 */
	private ActionFoodDto createActionFoodEntity(String foodId,
			String stapleFood, String foodPic, String foodName, String actionId) {
		ActionFoodDto actionFoodDto = new ActionFoodDto(foodId, actionId);
		
		actionFoodDto.setStapleFood(StapleFood.getStapleFood(stapleFood));
		actionFoodDto.setFoodPic(foodPic);
		actionFoodDto.setFoodName(foodName);
		
		return actionFoodDto;
	}
	
	/**
	 * @功能 修改食物(暂时没用)
	 */
	@RequestMapping("/admin/modifyFoodInfo")
	public String doModifyFoodInfoByFoodId(HttpServletRequest request, HttpServletResponse response){
		
		String foodId = ParamConverter.assertNotEmpty(request.getParameter("foodId"));		
		String stapleFood = ParamConverter.assertNotEmpty(request.getParameter("stapleFood"));
		String foodPic = ParamConverter.assertNotEmpty(request.getParameter("foodPic"));
		String foodName = SystemUtil.convertSpecialMark(ParamConverter.assertNotEmpty(request.getParameter("foodName")));
		String actionId = ParamConverter.assertNotEmpty(request.getParameter("actionId"));
		
		ActionFoodDto actionFoodDto = createActionFoodEntity(foodId, stapleFood, foodPic, foodName, actionId);
		dineFoodService.doModifyActionFood(actionFoodDto); 
		
		return "testFoodDetal";
	}
	
	/**
	 * @功能 删除食物
	 */
	@RequestMapping("/admin/ajaxDeleteFood")
	@ResponseBody
	public Map<String, Object> deleteFoodByFoodId(HttpServletRequest request, HttpServletResponse response){
		String foodId = ParamConverter.assertNotEmpty(request.getParameter("foodId"));
		int reFoodId = ParamConverter.convertToInteger(SystemUtil.decrypt(foodId));

		Map<String, Object> deleteFoodMap = new HashMap<String, Object>();		
		if (dineFoodService.doDeleteActionFoodByFoodId(reFoodId)) {
			deleteFoodMap.put("result", SystemConstants.AJAX_SUCCESS);			
		} else {
			deleteFoodMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			deleteFoodMap.put("message", "缓存处理失败, 请稍后重试");
		}

		return deleteFoodMap;
	}
}
